package game.platformer.levels;

import game.platformer.Player;
import game.platformer.interfaces.MapScreen;
import game.platformer.platformer;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

/**
 *
 * @author sOaF
 */
public final class levelFive implements Level {
    //varblock-------------------------

    private platformer game;
    private boolean loaded;  //is the level on screen
  
    private int height, width;
    private levelDeathBox boundry;
    levelExit forwardExit;
    levelExit backwardExit;
    //platforms------------------------
    levelPlatform[] platforms;
    //---------------------------------
    //constructor================================

    public levelFive(platformer game) {
        //save paramiters
        this.game = game;

        //initialize lists
   //     exits = new ArrayList<levelExit>();

        //initialize data
        height = 1000;
        width = 2000;
        loaded = false;
    }
    //load up the textures=======================

    public void loadTextures() {
    }
    //level loading textures=====================

    private void loadExits() {
        forwardExit = new levelExit(50, 60, 75, 50, 0);//exit location, next level entrance location, levelID
        backwardExit = new levelExit(250, 60, 150, 50, 3);
    }

    public boolean isLoaded() {
        return loaded;
    }

    public void loadLevel(int entranceX, int entreanceY) {
        //references-------------------
        elementsTextureSet texset = platformer.getLevelManager().getElementsMainPlatform();
        MapScreen scr = platformer.getScreenManager().getMapScreen();

        //set background---------------
        Sprite temp = new Sprite(100, 50, texset.getBackground());
        scr.setBackground(new SpriteBackground(temp));

        //place the deathBox-----------
        boundry = new levelDeathBox(width, height);
        boundry.setLastEntryPoint(entranceX, entreanceY);
        boundry.place();

        //setup platforms--------------
        platforms = new levelPlatform[]{
            new levelPlatform(texset.getElementsMainPlatform(), 0, 800),
            new levelPlatform(texset.getElementsMainPlatform(), 128, 800),
            new levelPlatform(texset.getElementsMainPlatform(), 400, 600),
            new levelPlatform(texset.getElementsMainPlatform(), 700, 800),
            new levelPlatform(texset.getElementsMainPlatform(), 900, 600),
            new levelPlatform(texset.getElementsMainPlatform(), 700, 300),
            new levelPlatform(texset.getElementsMainPlatform(), 500, 150),
            new levelPlatform(texset.getElementsMainPlatform(), 200, 100),
            new levelPlatform(texset.getElementsMainPlatform(), 50, 100)
        };
        //place platforms--------------
        for (levelPlatform p : platforms) {
            p.place();
        }

        //load and place exit
        loadExits();
        forwardExit.place();
        backwardExit.place();

        //place the player-------------
        Player.getPlayer().place(entranceX, entreanceY);


    }

    public void unloadLevel() {
        //references-------------------
        final MapScreen scr = platformer.getScreenManager().getMapScreen();
        final PhysicsWorld pw = platformer.getPhysicsWorld();
        final IUpdateHandler removalHandler;
        //-----------------------------

        //this function is called by map when updateing a frame
        //we do this so that items arnt removed between updates
        removalHandler = new IUpdateHandler() {

            public void onUpdate(float pSecondsElapsed) {

                //pull the platforms from the mapscreen
                for (levelPlatform p : platforms) {
                    scr.detachChild(p.getSpriteRep());
                    pw.unregisterPhysicsConnector(p.getConnector());
                    pw.destroyBody(p.getBodyRep());
                }

                //kill the death box
                scr.detachChild(boundry);

                //kill exits
                scr.detachChild(forwardExit);
                scr.detachChild(backwardExit);
                //remove the handler
                scr.unregisterUpdateHandler(this);
            }

            public void reset() {
            }
        };

        //start the removal
        scr.registerUpdateHandler(removalHandler);
        loaded = false;
    }
    //accessors==================================

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getNumExits() {
        return 0;
    }
    //returns if the param collides with the platforms
//bullet stuff

    public boolean colidesWithPlatform(Sprite colideable) {
        for (levelPlatform p : platforms) {
            if (p.getSpriteRep().collidesWith(colideable)) {
                return true;
            }
        }
        return false;
    }
}
